import React, { Component } from 'react';
import AsyncStorage from '@react-native-community/async-storage';
import Intro from './intro';
import { connect } from 'react-redux';
import { StatusBar } from 'react-native';
import CodePush from 'react-native-code-push';
import Config from '../../config';

class IntroPage extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isFirst: false,
    };
  }
  async componentDidMount() {
    try {
      console.log(Config.CODE_PUSH_KEY);
      const message = await CodePush.checkForUpdate(Config.CODE_PUSH_KEY);
      // const message = await CodePush.sync(Config.CODE_PUSH_KEY);
      console.log('CodePush：', message);
    } catch (e) {
      console.log(e);
    }
    StatusBar.setBarStyle('dark-content');
    const isFirst = await this.getAsyncStorage();
    const { replace } = this.props.navigation;
    if (!isFirst) {
      AsyncStorage.setItem('__FIRST__', 'first');
      this.setState({
        isFirst: true,
      });
    } else {
      //修改自动登录逻辑, 已有Token的情况下不再跳转到登录页面
      // this.judgeAutoLogin();
      const Token = await AsyncStorage.getItem('Authorization');
      if (Token) {
        replace('HomePage');
      } else {
        replace('Login');
      }
    }
  }
  getAsyncStorage = async () => {
    return await AsyncStorage.getItem('__FIRST__');
  };
  render() {
    return this.state.isFirst ? <Intro {...this.props} /> : null;
  }
}

export default connect()(IntroPage);
